package com.hsd.znsh.service.impl;

import com.hsd.znsh.entity.Event;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.ReadStatusEnum;
import com.hsd.znsh.repository.EventRepository;
import com.hsd.znsh.repository.HostManagerRepository;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-02 18:38:04
 */
@Service
public class EventServiceImpl extends BaseServiceImpl implements EventService {

    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private HostManagerRepository hostManagerRepository;

    @Override
    public List<Event> getEventList(User user, Integer readStatus,Integer page) {
        List<String> hostSnList=hostRepository.findHostSnListByManagerId(user.getCreateUserId());
        List<Integer> hostIds=hostManagerRepository.findBySecondManagerId(user.getId());

        if(hostIds!=null&&hostIds.size()>0) hostSnList.addAll(hostRepository.findHostSnListByIdIn(hostIds));
        Pageable pageable=new PageRequest(page-1,20);
        return eventRepository.findByGatewaySnInAndReadStatusOrderByCreateDateTimeDesc(hostSnList,readStatus,pageable);
    }

    @Override
    public Event getEventInfo(Integer id) {
        Event event=eventRepository.findOne(id);
        event.setReadStatus(ReadStatusEnum.READ.getCode());
        return eventRepository.save(event );
    }
}
